home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / auth / http.auth.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  7.1 KB  |  222 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Set of functions used to run http authentication.
  5.  * NOTE: Requires PHP loaded as a Apache module.
  6.  *
  7.  * @version $Id: http.auth.lib.php 10893 2007-11-01 20:59:48Z lem9 $
  8.  */
  9.  
  10.  
  11. /**
  12.  * Displays authentication form
  13.  *
  14.  * @global  string    the font face to use in case of failure
  15.  * @global  string    the default font size to use in case of failure
  16.  * @global  string    the big font size to use in case of failure
  17.  *
  18.  * @return  boolean   always true (no return indeed)
  19.  *
  20.  * @access  public
  21.  */
  22. function PMA_auth() {
  23.  
  24.     /* Perform logout to custom URL */
  25.     if (!empty($_REQUEST['old_usr']) && !empty($GLOBALS['cfg']['Server']['LogoutURL'])) {
  26.         PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
  27.         exit;
  28.     }
  29.  
  30.     if (empty($GLOBALS['cfg']['Server']['verbose'])) {
  31.         $server_message = $GLOBALS['cfg']['Server']['host'];
  32.     } else {
  33.         $server_message = $GLOBALS['cfg']['Server']['verbose'];
  34.     }
  35.     // remove non US-ASCII to respect RFC2616
  36.     $server_message = preg_replace('/[^\x20-\x7e]/i', '', $server_message);
  37.     header('WWW-Authenticate: Basic realm="phpMyAdmin ' . $server_message .  '"');
  38.     header('HTTP/1.0 401 Unauthorized');
  39.     if (php_sapi_name() !== 'cgi-fcgi') {
  40.     header('status: 401 Unauthorized');
  41.     }
  42.  
  43.     // Defines the charset to be used
  44.     header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
  45.     /* HTML header */
  46.     $page_title = $GLOBALS['strAccessDenied'];
  47.     require './libraries/header_meta_style.inc.php';
  48.     ?>
  49. </head>
  50. <body>
  51. <?php if (file_exists('./config.header.inc.php')) {
  52.           require './config.header.inc.php';
  53.       }
  54.  ?>
  55.  
  56. <br /><br />
  57. <center>
  58.     <h1><?php echo sprintf($GLOBALS['strWelcome'], ' phpMyAdmin ' . PMA_VERSION); ?></h1>
  59. </center>
  60. <br />
  61. <div class="warning"><?php echo $GLOBALS['strWrongUser']; ?></div>
  62.  
  63. <?php if (file_exists('./config.footer.inc.php')) {
  64.          require './config.footer.inc.php';
  65.       }
  66.  ?>
  67.  
  68. </body>
  69. </html>
  70.     <?php
  71.     exit();
  72. } // end of the 'PMA_auth()' function
  73.  
  74.  
  75. /**
  76.  * Gets advanced authentication settings
  77.  *
  78.  * @global  string    the username if register_globals is on
  79.  * @global  string    the password if register_globals is on
  80.  * @global  array     the array of server variables if register_globals is
  81.  *                    off
  82.  * @global  array     the array of environment variables if register_globals
  83.  *                    is off
  84.  * @global  string    the username for the ? server
  85.  * @global  string    the password for the ? server
  86.  * @global  string    the username for the WebSite Professional server
  87.  * @global  string    the password for the WebSite Professional server
  88.  * @global  string    the username of the user who logs out
  89.  *
  90.  * @return  boolean   whether we get authentication settings or not
  91.  *
  92.  * @access  public
  93.  */
  94. function PMA_auth_check()
  95. {
  96.     global $PHP_AUTH_USER, $PHP_AUTH_PW;
  97.     global $old_usr;
  98.  
  99.     // Grabs the $PHP_AUTH_USER variable whatever are the values of the
  100.     // 'register_globals' and the 'variables_order' directives
  101.     // loic1 - 2001/25/11: use the new globals arrays defined with php 4.1+
  102.     if (empty($PHP_AUTH_USER)) {
  103.         if (PMA_getenv('PHP_AUTH_USER')) {
  104.             $PHP_AUTH_USER = PMA_getenv('PHP_AUTH_USER');
  105.         } elseif (PMA_getenv('REMOTE_USER')) {
  106.             // CGI, might be encoded, see below
  107.             $PHP_AUTH_USER = PMA_getenv('REMOTE_USER');
  108.         } elseif (PMA_getenv('REDIRECT_REMOTE_USER')) {
  109.             // CGI, might be encoded, see below
  110.             $PHP_AUTH_USER = PMA_getenv('REDIRECT_REMOTE_USER');
  111.         } elseif (PMA_getenv('AUTH_USER')) {
  112.             // WebSite Professional
  113.             $PHP_AUTH_USER = PMA_getenv('AUTH_USER');
  114.         } elseif (PMA_getenv('HTTP_AUTHORIZATION')) {
  115.             // IIS, might be encoded, see below
  116.             $PHP_AUTH_USER = PMA_getenv('HTTP_AUTHORIZATION');
  117.         } elseif (PMA_getenv('Authorization')) {
  118.             // FastCGI, might be encoded, see below
  119.             $PHP_AUTH_USER = PMA_getenv('Authorization');
  120.         }
  121.     }
  122.     // Grabs the $PHP_AUTH_PW variable whatever are the values of the
  123.     // 'register_globals' and the 'variables_order' directives
  124.     // loic1 - 2001/25/11: use the new globals arrays defined with php 4.1+
  125.     if (empty($PHP_AUTH_PW)) {
  126.         if (PMA_getenv('PHP_AUTH_PW')) {
  127.             $PHP_AUTH_PW = PMA_getenv('PHP_AUTH_PW');
  128.         } elseif (PMA_getenv('REMOTE_PASSWORD')) {
  129.             // Apache/CGI
  130.             $PHP_AUTH_PW = PMA_getenv('REMOTE_PASSWORD');
  131.         } elseif (PMA_getenv('AUTH_PASSWORD')) {
  132.             // WebSite Professional
  133.             $PHP_AUTH_PW = PMA_getenv('AUTH_PASSWORD');
  134.         }
  135.     }
  136.  
  137.     // Decode possibly encoded information (used by IIS/CGI/FastCGI)
  138.     if (strcmp(substr($PHP_AUTH_USER, 0, 6), 'Basic ') == 0) {
  139.         $usr_pass = base64_decode(substr($PHP_AUTH_USER, 6));
  140.         if (!empty($usr_pass) && strpos($usr_pass, ':') !== false) {
  141.             list($PHP_AUTH_USER, $PHP_AUTH_PW) = explode(':', $usr_pass);
  142.         }
  143.         unset($usr_pass);
  144.     }
  145.  
  146.     // User logged out -> ensure the new username is not the same
  147.     if (!empty($old_usr)
  148.         && (isset($PHP_AUTH_USER) && $old_usr == $PHP_AUTH_USER)) {
  149.         $PHP_AUTH_USER = '';
  150.         // -> delete user's choices that were stored in session 
  151.         session_destroy(); 
  152.     }
  153.  
  154.     // Returns whether we get authentication settings or not
  155.     if (empty($PHP_AUTH_USER)) {
  156.         return false;
  157.     } else {
  158.         return true;
  159.     }
  160. } // end of the 'PMA_auth_check()' function
  161.  
  162.  
  163. /**
  164.  * Set the user and password after last checkings if required
  165.  *
  166.  * @global  array     the valid servers settings
  167.  * @global  integer   the id of the current server
  168.  * @global  array     the current server settings
  169.  * @global  string    the current username
  170.  * @global  string    the current password
  171.  *
  172.  * @return  boolean   always true
  173.  *
  174.  * @access  public
  175.  */
  176. function PMA_auth_set_user()
  177. {
  178.     global $cfg, $server;
  179.     global $PHP_AUTH_USER, $PHP_AUTH_PW;
  180.  
  181.     // Ensures valid authentication mode, 'only_db', bookmark database and
  182.     // table names and relation table name are used
  183.     if ($cfg['Server']['user'] != $PHP_AUTH_USER) {
  184.         $servers_cnt = count($cfg['Servers']);
  185.         for ($i = 1; $i <= $servers_cnt; $i++) {
  186.             if (isset($cfg['Servers'][$i])
  187.                 && ($cfg['Servers'][$i]['host'] == $cfg['Server']['host'] && $cfg['Servers'][$i]['user'] == $PHP_AUTH_USER)) {
  188.                 $server        = $i;
  189.                 $cfg['Server'] = $cfg['Servers'][$i];
  190.                 break;
  191.             }
  192.         } // end for
  193.     } // end if
  194.  
  195.     $cfg['Server']['user']     = $PHP_AUTH_USER;
  196.     $cfg['Server']['password'] = $PHP_AUTH_PW;
  197.  
  198.     return true;
  199. } // end of the 'PMA_auth_set_user()' function
  200.  
  201.  
  202. /**
  203.  * User is not allowed to login to MySQL -> authentication failed
  204.  *
  205.  * @return  boolean   always true (no return indeed)
  206.  *
  207.  * @access  public
  208.  */
  209. function PMA_auth_fails()
  210. {
  211.     $error = PMA_DBI_getError();
  212.     if ($error && $GLOBALS['errno'] != 1045) {
  213.         PMA_fatalError($error);
  214.     } else {
  215.         PMA_auth();
  216.         return true;
  217.     }
  218.  
  219. } // end of the 'PMA_auth_fails()' function
  220.  
  221. ?>
  222.